package com.dhcc.DRGsHosp.application.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.querychannel.JdbcTemplateObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.SumExpenseApplication;

/**
 * 
 * @ClassName: SumExpenseApplicationImpl  
 * @Description ：    费用中间表计算
 * @Date: 2017年9月19日 下午5:26:34  
 * @Author LiuHongJun 
 * @Version 3.0.0 DHC-DRGs
 */
@Named("sumExpenseApplicationImplqiaoyilong20181026")
@Transactional
public class SumExpenseApplicationImpl implements SumExpenseApplication {
	   @Autowired  
	   private JdbcTemplateObj jdbcTemplateObj;
/**
 * 
 *  @Description：获取病例id
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.application.SumExpenseApplication#findMrParid(java.lang.String, java.lang.String)
 */
	@Override
	public List<Map<String, Object>> findMrParid(String dateFrom, String dateTo) {
		String sql="SELECT a.pmainindex ID FROM drgs.MR_BASEINFO a WHERE to_char( a.PUSH_DATE,'yyyy-mm-dd') BETWEEN '"+dateFrom+"' AND '"+dateTo+"'  AND a.pmainindex  IS NOT NULL";
		return jdbcTemplateObj.queryForList(sql);
	}

/**
 * 
 *  @Description：调用存储过程
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.application.SumExpenseApplication#sumExpense(java.lang.String)
 */
	@Override
	public void sumExpense(String id) {
		 Connection connection = null;
		try {
			connection=jdbcTemplateObj.getDataSource().getConnection();
			connection.setAutoCommit(true);
		    CallableStatement statement = connection.prepareCall("call drgs.proc_expense_sum(?)");	
		     statement.setString(1, id);
			 statement.execute();	
		} catch (SQLException e) {
			e.printStackTrace();
		} finally { 
			  try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} 
		} 
		
	}

	/**
	 * 
	 *  @Description：根据出院日期查询id
	 *  @Author ZouYiyu  
	 *  @see com.dhcc.DRGsHosp.application.SumExpenseApplication#findMrParidByOut(java.lang.String, java.lang.String)
	 */
@Override
public List<Map<String, Object>> findMrParidByOut(String dateFrom, String dateTo) {
	String sql=" SELECT a.pmainindex ID FROM drgs.MR_BASEINFO a \n";
	sql+=" left join drgs.mr_ipinfo i on a.pmainindex=i.pmainindex \n";
	sql+=" WHERE to_char( i.P00290000,'yyyy-mm-dd') BETWEEN '"+dateFrom+"' AND '"+dateTo+"'  AND a.pmainindex  IS NOT NULL ";
	return jdbcTemplateObj.queryForList(sql);
}
/**
 * 
 *  @Description：处理特殊编码
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.application.SumExpenseApplication#handelSpecCode()
 */
	@Override
	public void handelSpecCode() {
		 Connection connection = null;
		try {
			connection=jdbcTemplateObj.getDataSource().getConnection();
			connection.setAutoCommit(true);
		   CallableStatement statement = connection.prepareCall("call drgs.PROC_REPLACE_CODE()");	
			 statement.execute();	
		} catch (SQLException e) {
			e.printStackTrace();
		} finally { 
			  try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} 
		} 
		
	} 
}
